<?php
/* Description: This file is used to edit product. */
#########IncludeFiles####################################################################################
include_once '../inc/config.php';
include_once("../inc/bootstrape.php");
require_once _SITE_PATH_.'classes/Vendor/Vendor.php';
require_once _SITE_PATH_.'classes/Category/Category.php';
require_once _SITE_PATH_.'classes/Product/Product.php';
require_once _SITE_PATH_.'classes/CommonUtility/CommonUtility.php';

$errMsg = $_GET['err'];
$action = trim($_REQUEST['action']);
$id = trim($_REQUEST['id']); 
if(empty($id)){
    header("Location: "._SITE_ADMIN_URL_."product/product_mng.php");
    exit;
}

if( $_POST['submit']=='Submit'){    
	$parms['id'] 						= $_POST['id'];
    $parms['product_name'] 				= $_POST['product_name'];
	$parms['product_category']			= $_POST['product_category'];
	$parms['product_subcategory'] 		= $_POST['product_subcategory'];
	$parms['product_subsubcategory']	= $_POST['product_subsubcategory'];
	$parms['description'] 				= $_POST['description'];
	if(!empty($_POST['attributes'])){
		$parms['attributes'] 		= implode(',',$_POST['attributes']);
	}
	$parms['tags'] 						= $_POST['tags'];
	$parms['added']						= date("Y-m-d h:i:s");
	$parms['modified'] 					= date("Y-m-d h:i:s");
	
	// upload and create thumb of the image //
    if( !empty( $_FILES['product_image'] ) ){
        $filename  = 'product_image';     //  this is name of the control you are using in html form         
        $module='product';
        $uploadedfile = CommonUtility::uploadNcreateThumb($filename, $module, true, 100);
        if( $uploadedfile!= -1 )    {
            $parms['product_image'] = $uploadedfile;//$_POST['package_image'];
        }
        CommonUtility::removeImage($result['product_image'], $module); 
    }
    
    $condition = array(); 
    $condition['id'] = $parms['id'];
    if( CommonUtility::updateData($parms, 'product', $condition)) {        
		//  add product attributes
        //$parms = array();
//        $parms['product_id'] = $id ;
//        CommonUtility::deleteData('product_attributes',$parms);

           //below lines are comment for removing attributes from master product
//         $parms_condition = array();        
//         if(!empty($_POST['attributes'])){
//             foreach($_POST['attributes'] as $key => $val){
//                 $parms_condition['product_id'] = $id;
//                 $parms_condition['attribute_id'] = $key;
//                 foreach($val as $inner_key => $inner_val){
//                     $parms_attributes['attribute_name'] = $inner_key;
//                     $parms_attributes['attribute_value'] = $inner_val;
//                 }
//                 CommonUtility::updateData($parms_attributes, 'product_attributes',$parms_condition);
//             }
//         }
        
        header("Location: product_mng.php?msg=1206");            
        exit;
    }
    $errMsg = 1207; 
}

$editor = CommonUtility::includeEditor();

$product_attributes = array();
$product_attributes = Product::getProductAttributesNew($id);

if( $err ){
    $result = $_POST;
}else{
    $products = Product::getProductById($id);
	$result = $products[0];
}

$arrProductAttributes = array();
if($result['attributes'] != ''){
	$str_attributes = $result['attributes'];
	$arrProductAttributes = explode(',',$str_attributes);
}

$main_categories = array();
$main_categories = Category::getMainCategories();

$sub_categories = array();
$sub_categories = Category::getSubCategories($result['product_category']);

$sub_sub_categories = array();
$sub_sub_categories = Category::getSubSubCategories($result['product_subsubcategory'],1);
$subsubcat = $sub_sub_categories[0];

// $drop_down_attributes = array();
// $drop_down_attributes = CommonUtility::getProductAttributes($arrProductAttributes);

$errMsg = $err;
$_tplName = 'product/product_edit.tpl';
$_title = 'Edit Product';

require_once _ADMIN_TPL_MAIN_;
?>